package org.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class TestHbase2 {

    private Connection conn;
    private Statement stat;
    private ResultSet rs;

    // 开启连接
    public void init() {
        try {
            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
            conn = DriverManager.getConnection("jdbc:phoenix:192.168.88.101:2181");
            stat = conn.createStatement();
            System.out.println("连接成功!");
        } catch (Exception e) {
            // exception
            System.out.println(e);
        }
    }

    // 创建表
    public void createTable(String tableName) {
        String sql = "create table " + tableName
                + "( name varchar primary key,"
                + "id unsigned_int,"
                + "guikou varchar,"
                + "dizhi varchar,"
                + "diyu varchar,"
                + "wangzhi varchar,"
                + "emill varchar,"
                + "faren varchar,"
                + "youzheng varchar,"
                + "phone varchar,"
                + "lianxiren varchar,"
                + "chuanzhen varchar,"
                + "shuxing varchar,"
                + "jianjie varchar,"
                + "xuqiu varchar,"
                + "xuqiushixian varchar,"
                + "xuqiugaishu varchar,"
                + "guanjianzi varchar,"
                + "jine varchar,"
                + "fangshi varchar,"
                + "huodongleixing varchar,"
                + "danwei varchar)";
        // 执行
        try {
            stat.execute(sql);
            System.out.println("创建表成功");
        } catch (Exception e) {
            // handle exception
            System.out.println(e);
        }
    }

    // 增加数据
    public void upsertData() {
        ArrayList<String> dataList = new ArrayList<String>();

        dataList.add("upsert into jigou values('铁大', 1, '软工', '铁大', '长安区', 'stdu.enu.cn', '323@qq.com', " +
                "'小明', '650000', '18045685212675', '小红', '451482', '高等院校', '铁道兵精神', 'hbase的使用', '2023-9-15'," +
                " '啥也不会', 'hbase', '1000', '独立研发', '应用研究', '软工')");


        for (String sql : dataList) {
            try {
                stat.execute(sql);
                conn.commit();
                System.out.println("插入数据成功");
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }

    // 删除数据
    public void deleteData(String tableName, int id) {
        // 根据id删除数据
        String sql = "delete from " + tableName + " where id = " + id;
        try {
            stat.execute(sql);
            conn.commit();
            System.out.println("删除数据成功");
        } catch (Exception e) {
            System.out.println(e);
        }
    }


    // 查询数据
    public void selectData() {
        // 查询所有数据
        String sql = "select * from jigou";
        try {
            // 保存结果集
            rs = stat.executeQuery(sql);
            System.out.println("xuqiu" + "\t" + "name");
            while (rs.next()) {

                System.out.println(rs.getString("xuqiu") + "\t" + rs.getString("name") );
            }
            conn.commit();
            System.out.println("查询数据成功");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    // 删除表
    public void dropTable(String tableName) {
        String sql = "drop table " + tableName;
        try {
            stat.execute(sql);
            conn.commit();
            System.out.println("删除表成功");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    // 关闭连接
    public void closeConn() {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stat != null){

                stat.close();
            }
            if (conn != null){
                conn.close();
            }
            System.out.println("关闭成功");
        } catch (Exception e) {
            System.out.println(e);
        }
    }


}


